<?php
class Subscriber extends AppModel {

	var $name = 'Subscriber';
	var $validate = array(
		'email' => array(
				'Valid'=>array('rule'=>VALID_EMAIL,'message'=>'Please enter your email address'),
				'Unique'=> array('rule' => array('uniqueEmail'), 'message' => 'The email already exists.')
			)
		);
	
	var $fieldProperties  = array(
		'name'=>array('group'=>"add",'fieldModifier'=>'formatField'),
		'email'=>array('group'=>'add','fieldModifier'=>'formatField'),
	);

	var $actsAs = array(
		'Cms.Metric' => array(
			array('metric' => 'NumRecords')
		)
	);
	
	const ACTIVE = 1;
	const INACTIVE = 0;
	function mailType() {
		if($this->data['Subscriber']['type'] == '')
			$this->data['Subscriber']['type'] = 'html';
		return true; 
	}
	
	function beforeSave($options)
	{
		
		if($this->id ==null && !isset($this->data[$this->alias]['id']))
		{
			$key = time().(isset($this->data[$this->alias]['email'])? $this->data[$this->alias]['email'] : $this->data[$this->alias]['entity'].$this->data[$this->alias]['entity_id']); 
			$key = md5($key);
			$this->data[$this->alias]['code'] = $key;
					
			if(cr('NewsletterConfig.verify_email') == 1 || is_null(cr('NewsletterConfig.verify_email')))
			{
				$this->data[$this->alias]['status'] = self::INACTIVE;
			}else
			{
				$this->data[$this->alias]['status'] = self::ACTIVE;
			}
			if(isset($this->data[$this->alias]['entity']) && $this->data[$this->alias]['entity'] != '')
			{
				$this->data[$this->alias]['status'] = self::ACTIVE;	
			}	
			
		}
		return parent::beforeSave($options);
	}
	/**
	 * 
	 * @param $value
	 * @param $action
	 * @param $result
	 * @param $fieldName
	 * @return unknown_type
	 */
	function formatField($value,$action, $result, $fieldName)
	{
		if($action=='save')
		{
			return $value;
		}
		if(!isset($result[$this->alias]['entity']) || $result[$this->alias]['entity']==null)
		{
			return $value;
		}
		$Model = $this->getSubsciberModel($result[$this->alias]['entity']);
		if($Model==false)
		{
			return $value;
		}
		$searchField = $fieldName =="name" ? $Model->displayField : $fieldName;	
		$field = $Model->field($searchField,array('id'=>$result[$this->alias]['entity_id']));
		return $field;
	}
	
	function getSubsciberModel($alias=null)
	{
		if($alias==null || in_array($alias,cr('Newsletter.allowedModelAlias'))==false)
		{
			return false;
		}
		$alias = Inflector::camelize($alias);
		if(isset($this->{$alias}) && is_object($this->{$alias}))
		{
			$model = $this->{$alias};
		}
		else
		{
			$model =  Utility::getModelObject($alias);
		}
		return $model;
	}
	
	function uniqueEmail($data)
	{
		if(parent::unique($data)==false)
		{
			$this->data[$this->alias]['status'] = self::ACTIVE;
			$this->data[$this->alias]['id'] = $this->field('id',array($data));
			$this->id = $this->data[$this->alias]['id'];
			$this->exists();
		}
		return true;
	}
	
	function beforeValidate($options)
	{
		if(isset($this->data[$this->alias]['entity']) && isset($this->data[$this->alias]['entity_id']) && is_null($this->data[$this->alias]['entity_id']) ==false)
		{
			$id = $this->field('id',array_intersect_key($this->data[$this->alias],array('entity'=>'','entity_id'=>'')));
			if($id)
			{
				$this->data[$this->alias]['id'] = $id;
				$this->id = $id;
				$this->exists();
			}
		}
		return parent::beforeValidate($options);
	}
}
 ?>